// pages/show-locations-page/show-locations-page.js
Page({
    data: {
        showLoadingState:false,
        //搜索栏数据
        p0:"https://pic.imgdb.cn/item/62f9aa2a16f2c2beb174caea.png",
        p1:"https://pic.imgdb.cn/item/63144c8e16f2c2beb10e7e39.png",

        //数据转换
        type_room:[['室内'],['室外']],
        type_free:[['付费'],['免费']],
        state:[['下架'],['上架']],
        //场馆具体信息
        p2:"https://pic.imgdb.cn/item/62f0f2c616f2c2beb1515804.png",
        p3:"https://pic.imgdb.cn/item/62f0f2c616f2c2beb151580c.png",
        p4:"https://pic.imgdb.cn/item/62f0f2c616f2c2beb1515841.png",
        p5:"https://pic.imgdb.cn/item/62f0f2c616f2c2beb1515851.png",

        locationName:"",

        //以下locations里的元素，按照上架的场馆在先，下架的场馆在后的顺序排列，其他不限
        locations:[
            {
                location_id:12,
                location_name:'大学生活动中心篮球场',
                location_type_room:0,//室内/室外——0/1
                location_type_free:1,//付费/免费——0/1
                location_open_time:14,
                location_close_time:19,
                location_state:1,//上架/下架——1/0
                location_price:10,
                sports:2,
            },
            {
                location_id:12,
                location_name:'大学生活动中心篮球场',
                location_type_room:0,//室内/室外——0/1
                location_type_free:1,//付费/免费——0/1
                location_open_time:14,
                location_close_time:19,
                location_state:1,//上架/下架——1/0
                location_price:10,
                sports:2,
            },
            {
                location_id:12,
                location_name:'大学生',
                location_type_room:0,//室内/室外——0/1
                location_type_free:1,//付费/免费——0/1
                location_open_time:14,
                location_close_time:19,
                location_state:1,//上架/下架——1/0
                location_price:10,
                sports:2,
            },
            {
                location_id:12,
                location_name:'大学生活动中心篮球场',
                location_type_room:0,//室内/室外——0/1
                location_type_free:1,//付费/免费——0/1
                location_open_time:14,
                location_close_time:19,
                location_state:1,//上架/下架——1/0
                location_price:10,
                sports:2,
            },
            {
                location_id:12,
                location_name:'大学生活动中心篮球场',
                location_type_room:0,//室内/室外——0/1
                location_type_free:1,//付费/免费——0/1
                location_open_time:14,
                location_close_time:19,
                location_state:1,//上架/下架——1/0
                location_price:10,
                sports:2,
            },
            {
                location_id:12,
                location_name:'大学生活动中心篮球场',
                location_type_room:0,//室内/室外——0/1
                location_type_free:1,//付费/免费——0/1
                location_open_time:14,
                location_close_time:19,
                location_state:1,//上架/下架——1/0
                location_price:10,
                sports:2,
            },
            {
                location_id:12,
                location_name:'大学生活动中心篮球场',
                location_type_room:0,//室内还是室外
                location_open_time:14,
                location_close_time:19,
                location_state:0,//上架还是下架
                location_price:10,
            },
            {
                location_id:12,
                location_name:'大学生活动中心篮球场',
                location_type_room:0,//室内还是室外
                location_open_time:14,
                location_close_time:19,
                location_state:0,//上架还是下架
                location_price:10,
            },
            {
                location_id:12,
                location_name:'大学生活动中心篮球场',
                location_type_room:0,//室内还是室外
                location_open_time:14,
                location_close_time:19,
                location_state:0,//上架还是下架
                location_price:10,
            }
        ]
    },
    onShow(options) {
        //从服务器获取数据，然后初始化数值
        var that = this;
        wx.request({
            url: getApp().globalData.serverUrl,
            method:"GET",
            timeout:10000,
            data:{
              //各种数据的传送
              operation:"SHOW_LOCATION",
              studentId:getApp().globalData.userInfo.studentId,
            },
            success(res){
              if(res.data == false){
                  wx.showModal({
                      showCancel:false,
                      title:"请求失败,请重试",
                  });
              }
              else{
                  //成功获取到信息后的操作
                  console.log(res);
                  that.setData({
                    "locations":res.data.ll,
                  });
              }
            },
            fail(res){
              console.log(res);
              wx.showModal({
                  showCancel:false,
                  title:"请求超时,请重试",
              });
            },
          });
    },
    onChangeInput:function(e){
        this.setData({
            'locationName':e.detail.value,
        })
    },
    onSearchLocation:function(e){
        //从服务器获取结果，然后展示
        var that = this;
        that.onShowLoading()
        wx.request({
            url: getApp().globalData.serverUrl,
            method:"GET",
            timeout:10000,
            data:{
              //各种数据的传送
              operation:"SEARCH_LOCATION",
              studentId:getApp().globalData.userInfo.studentId,
              locationName:that.data.locationName,
            },
            success(res){
              if(res.data == false){
                  that.onHideLoading()
                  wx.showModal({
                      showCancel:false,
                      title:"请求失败,请重试",
                  });
              }
              else{
                  //成功获取到信息后的操作
                  that.onHideLoading()
                  console.log(res);
                  that.setData({
                      'locations':res.data.ll,
                  })
              }
            },
            fail(res){
                that.onHideLoading()
              wx.showModal({
                  showCancel:false,
                  title:"请求超时,请重试",
              });
            },
          });
    },
    toPage_detailed_location_page:function(e){
        var location_id = e.currentTarget.dataset.id;
        var location_name = e.currentTarget.dataset.name;
        console.log(location_id+"  "+location_name);
        wx.navigateTo({
          url: '/pages/detailed-location-page/detailed-location-page?locationId='+location_id+"&&locationName="+location_name,
        })
    },
    toPage_new_location_page:function(e){
        wx.navigateTo({
          url: '/pages/new-location-page/new-location-page',
        })
    },
    onShowLoading:function(e){
        var that = this;
        wx.showLoading({
          title: '加载中',
          mask:true,
          success(res){
              that.setData({
                  "showLoadingState":true,
              })
          }
        })
        setTimeout(that.onHideLoading,10200)
    },
    onHideLoading:function(e){
        var that = this;
        if(that.data.showLoadingState == true){
            wx.hideLoading();
            that.setData({ 
                "showLoadingState":false,
            })
        }
    },
})